package com.supermap.imanager.notification.impl;

import java.io.File;
import java.io.IOException;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.supermap.imanager.notification.Notification;
import com.supermap.imanager.notification.commontypes.AppAlarmItemEntity;
import com.supermap.imanager.notification.commontypes.AppRecoveredAlarmItemEntity;
import com.supermap.imanager.notification.commontypes.NotificationEntity;
import com.supermap.imanager.notification.commontypes.NotificationItemEntity;
import com.supermap.imanager.notification.commontypes.URLAlarmItemEntity;
import com.supermap.imanager.notification.commontypes.URLRecoveredAlarmItemEntity;
import com.supermap.utils.XMLManager;

public class ImocConsoleNotification implements Notification {

	private final String logPath = "/etc/imanager/Log.xml";
	private XMLManager xmlManager = null;

	private boolean enable = true;

	public void setEnable(boolean enable) {
		this.enable = enable;
	}

	@Override
	public void send(NotificationEntity entity) {
		if (enable) {

			File file = new File(logPath);
			if ( !file.exists() ) {
				xmlManager = new XMLManager(logPath);
			}
			// 通知标题
			System.out.println("title:" + entity.title);
			// 通知内容的类型
			System.out.println("type:" + entity.type.toString());
			System.out.println("contents");
			List<NotificationItemEntity> contents = entity.contents;
			contents.forEach((content) -> {
				System.out.println("star send way ..........................................");
				printItem(content);
			});
		}
	}

	private void printItem(NotificationItemEntity content) {
		// 打印报警描述信息
		System.out.println("description:" + content.description);
		// 根据需要还可获取通知的详细信息:
		// app告警项
		if (content instanceof AppAlarmItemEntity) {
			System.out.println("app alarm ................................");
			AppAlarmItemEntity item = (AppAlarmItemEntity) content;
			System.out.println("appId:" + item.appId);// app的id
			System.out.println("name:" + item.name);// app的名称
			System.out.println("address:" + item.address);// app的地址
			System.out.println("type:" + item.type);// app类型；iServer/iPortal
			System.out.println("suggest:" + item.suggest);// 建议内容
			System.out.println("description:" + item.description);// 告警描述信息
			System.out.println("triggerid: "+item.triggerid);//告警唯一id标识

			// 上传imoc告警
			String alarm_description = "app: " +item.name +" address: "+item.address +" description: "+item.description+ " triggerid: "+item.triggerid;
			JSONObject result = ImocAlerm.getAlerm(item.name, alarm_description, System.currentTimeMillis() + "", "2",
					true);
			if(result.getString("code") == "0"){
				try {
					xmlManager.write(logPath, item.triggerid, item.name, item.type, item.address, alarm_description);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		// URL告警项
		else if (content instanceof URLAlarmItemEntity) {
			System.out.println("url alarm ................................");
			URLAlarmItemEntity item = (URLAlarmItemEntity) content;
			System.out.println("name:" + item.name);// 告警名称
			System.out.println("address:" + item.address);// URL地址
			System.out.println("expectedStatus:" + item.expectedStatus);// 预期状态
			System.out.println("actualStatus:" + item.actualStatus);// 实际状态
			System.out.println("suggest:" + item.suggest);// 建议内容
			System.out.println("description:" + item.description);// 告警描述信息
			System.out.println("triggerid: "+item.triggerid);//告警唯一id标识

			// 上传imoc告警
			String alarm_description = "url: "+item.address +" actualStatus isn't 200, please check it."+ " triggerid: "+item.triggerid;
			ImocAlerm.getAlerm(item.name, alarm_description, System.currentTimeMillis() + "", "2",
					true);
		} // app告警恢复项
		else if (content instanceof AppRecoveredAlarmItemEntity) {
			System.out.println("app ok ................................");
			AppRecoveredAlarmItemEntity item = (AppRecoveredAlarmItemEntity) content;
			System.out.println("appId:" + item.appId);// app的id
			System.out.println("name:" + item.name);// app的名称
			System.out.println("address:" + item.address);// app的地址
			System.out.println("type:" + item.type);// app类型；iServer/iPortal
			System.out.println("alarmValue:" + item.alarmValue);// 告警阈值
			System.out.println("startTime:" + item.startTime);// 开始时间
			System.out.println("endTime:" + item.endTime);// 结束时间
			System.out.println("durationTime:" + item.durationTime);// 持续时间
			System.out.println("description:" + item.description);// 告警描述信息
			System.out.println("triggerid: "+item.triggerid);//告警唯一id标识

			String des = xmlManager.reader(item.triggerid, logPath);
			if ( des==null || "".equals(des) ){
				ImocAlerm.restoreAlerm(item.name, des, System.currentTimeMillis() + "", "2");
				xmlManager.removeElement(item.triggerid, logPath);
			}


		} // URL告警恢复项
		else if (content instanceof URLRecoveredAlarmItemEntity) {
			System.out.println("url ok ................................");
			URLRecoveredAlarmItemEntity item = (URLRecoveredAlarmItemEntity) content;
			System.out.println("description:" + item.name);// 告警名称
			System.out.println("description:" + item.url);// URL地址
			System.out.println("expectedStatus:" + item.expectedStatus);// 预期状态
			System.out.println("startTime:" + item.startTime);// 开始时间
			System.out.println("endTime:" + item.endTime);// 结束时间
			System.out.println("durationTime:" + item.durationTime);// 持续时间
			System.out.println("description:" + item.description);// 告警描述信息
			System.out.println("triggerid: "+item.triggerid);//告警唯一id标识

			String recovered_description = "url: "+item.url +" actualStatus isn't 200, please check it."+ " triggerid: "+item.triggerid;
			ImocAlerm.restoreAlerm(item.name, recovered_description, System.currentTimeMillis() + "", "2");
		}
	}

}
